package com.ez.supervise.action.biz;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.core.Result;
import com.ez.supervise.model.biz.GoalDetail;
import com.ez.supervise.service.GoalDetailService;
import com.ez.supervise.util.JsonUtils;

public class GoalDetailAction extends BaseJsonAction {

	private static final long serialVersionUID = -4886166027215278405L;
	private Long id;
	private GoalDetailService goalDetailService;
	private GoalDetail goalDetail;
	private Page page;

	public String saveGoalDetail() {
		try {
			if (goalDetail.isNew()) {
				goalDetailService.insert(goalDetail);
			} else {
				goalDetailService.update(goalDetail);
			}
			Result result = new Result(goalDetail.getId());
			result.setEntity(goalDetail);
			JsonConfig config = new JsonConfig();
			config.setExcludes(new String[] { "creator", "createTime",
					",modifier", "modifyTime", "isDeleted", "new" });
			outJsonString(JSONObject.fromObject(result, config).toString());
		} catch (Exception e) {
			e.printStackTrace();
			outString(RESULT_FAIL);
		}
		return SUCCESS;
	}
	public String auditGoalDetail() {
		try {
			GoalDetail auditGoalDetail = new GoalDetail(); 
			auditGoalDetail.setId(goalDetail.getId());
			auditGoalDetail.setStatus(goalDetail.getStatus());
			goalDetailService.update(auditGoalDetail);
			Result result = new Result(auditGoalDetail.getId());
			result.setEntity(auditGoalDetail);
			JsonConfig config = new JsonConfig();
			config.setExcludes(new String[] { "creator", "createTime",
					",modifier", "modifyTime", "isDeleted", "new" });
			outJsonString(JSONObject.fromObject(result, config).toString());
		} catch (Exception e) {
			e.printStackTrace();
			outString(RESULT_FAIL);
		}
		return SUCCESS;
	}

	public String findAllGoalDetail() {
		try {
			page = new Page();
			List result = goalDetailService.findAll();
			page.setRoot(result);
			page.setTotalProperty(result.size());
			outJsonString(JsonUtils.fromObject(page,
					new String[] { "creator", "createTime", ",modifier",
							"modifyTime", "isDeleted", "new" }));

		} catch (Exception e) {
			e.printStackTrace();
			outString(RESULT_FAIL);
		}
		return SUCCESS;
	}

	public String findGoalDetailByPage() {
		try {
		    page = new Page();
		    HttpServletRequest request = getRequest();
		    page.putCondition("year", NumberUtils.isNumber(request.getParameter("year")) ? Long.valueOf(request.getParameter("year")) : null);
		    page.putCondition("categoryId", NumberUtils.isNumber(request.getParameter("categoryId")) ? Long.valueOf(request.getParameter("categoryId")) : null);
		    page.putCondition("content", request.getParameter("content"));
		    page.putCondition("checkOrgId", NumberUtils.isNumber(request.getParameter("checkOrgId")) ? Long.valueOf(request.getParameter("checkOrgId")) : null);
		    String status = request.getParameter("status");
		    if(StringUtils.isNotBlank(status)){
		    	 page.putCondition("status", status);
		    }
		    int start = NumberUtils.toInt(request.getParameter("start"), 0);
		    int limit = NumberUtils.toInt(request.getParameter("limit"), 20);
		    page.setStart(start++);
		    page.setLimit(limit);
		    page.addPageInfo();
		    goalDetailService.findGoalDetailByPage(page);
		    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
			"isDeleted", "new" }));
		    
		} catch (Exception e) {
		    e.printStackTrace();
		    outString(RESULT_FAIL);
		}
		return SUCCESS;
	}
	
	public String queryGoalDetailByPage() {
		try {
		    page = new Page();
		    HttpServletRequest request = getRequest();
		    page.putCondition("year", NumberUtils.isNumber(request.getParameter("year")) ? Long.valueOf(request.getParameter("year")) : null);
		    page.putCondition("categoryId", NumberUtils.isNumber(request.getParameter("categoryId")) ? Long.valueOf(request.getParameter("categoryId")) : null);
		    page.putCondition("content", request.getParameter("content"));
		    page.putCondition("checkOrgId", NumberUtils.isNumber(request.getParameter("checkOrgId")) ? Long.valueOf(request.getParameter("checkOrgId")) : null);
		    String status = request.getParameter("status");
		    if(StringUtils.isNotBlank(status)){
		    	 page.putCondition("status", status);
		    }
		    int start = NumberUtils.toInt(request.getParameter("start"), 0);
		    int limit = NumberUtils.toInt(request.getParameter("limit"), 20);
		    page.setStart(start++);
		    page.setLimit(limit);
		    page.addPageInfo();
		    goalDetailService.queryGoalDetailByPage(page);
		    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
			"isDeleted", "new" }));
		    
		} catch (Exception e) {
		    e.printStackTrace();
		    outString(RESULT_FAIL);
		}
		return SUCCESS;
	}
	public String findGoalDetailApplyByPage(){
		try {
			page = new Page();
			HttpServletRequest request = getRequest();
			page.putCondition("year", NumberUtils.isNumber(request.getParameter("year")) ? Long.valueOf(request.getParameter("year")) : null);
			page.putCondition("categoryId", NumberUtils.isNumber(request.getParameter("categoryId")) ? Long.valueOf(request.getParameter("categoryId")) : null);
			page.putCondition("content", request.getParameter("content"));
			page.putCondition("checkOrgId", NumberUtils.isNumber(request.getParameter("checkOrgId")) ? Long.valueOf(request.getParameter("checkOrgId")) : null);
			page.putCondition("applyOrgId", NumberUtils.isNumber(request.getParameter("applyOrgId")) ? Long.valueOf(request.getParameter("applyOrgId")) : null);
			page.putCondition("status", "apply");
			int start = NumberUtils.toInt(request.getParameter("start"), 0);
			int limit = NumberUtils.toInt(request.getParameter("limit"), 20);
			page.setStart(start++);
			page.setLimit(limit);
			page.addPageInfo();
			goalDetailService.findGoalDetailApplyByPage(page);
			outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
					"isDeleted", "new" }));
			
		} catch (Exception e) {
			e.printStackTrace();
			outString(RESULT_FAIL);
		}
		return SUCCESS;
	}
	public String findGoalDetailAuditByPage(){
		try {
		    page = new Page();
		    HttpServletRequest request = getRequest();
		    page.putCondition("year", NumberUtils.isNumber(request.getParameter("year")) ? Long.valueOf(request.getParameter("year")) : null);
		    page.putCondition("categoryId", NumberUtils.isNumber(request.getParameter("categoryId")) ? Long.valueOf(request.getParameter("categoryId")) : null);
		    page.putCondition("status", request.getParameter("status"));
		    page.putCondition("applyOrgId", NumberUtils.isNumber(request.getParameter("applyOrgId")) ? Long.valueOf(request.getParameter("applyOrgId")) : null);
		      
		    int start = NumberUtils.toInt(request.getParameter("start"), 0);
		    int limit = NumberUtils.toInt(request.getParameter("limit"), 20);
		    page.setStart(start++);
		    page.setLimit(limit);
		    page.addPageInfo();
		    goalDetailService.findGoalDetailByPage(page);
		    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
			"isDeleted", "new" }));
		    
		} catch (Exception e) {
		    e.printStackTrace();
		    outString(RESULT_FAIL);
		}
		return SUCCESS;
	}
	public String findAll() {
		page = new Page();
		page.setTotalProperty(0);
		page.setRoot(goalDetailService.findAll());

		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { "creator", "createTime", ",modifier",
				"modifyTime", "isDeleted", "new", });

		JSONObject jsonObj = JSONObject.fromObject(page, config);
		// System.out.println(jsonObj.toString());
		outJsonString(jsonObj.toString());
		return SUCCESS;
	}

	/**
	 * @return
	 */
	public String deleteGoalDetail() {
		try {
			if (id != null && id > 0) {
				goalDetailService.delete(id);
			}
			outJsonString(RESULT_SUCCESS);
		} catch (Exception e) {
			outJsonString(RESULT_FAIL);
		}
		return SUCCESS;
	}

	/**
	 * @return
	 * @throws Exception
	 */
	public String updateGoalDetail() throws Exception {
		String fieldName = getRequest().getParameter("fieldName");
		String fieldValue = getRequest().getParameter("fieldValue");
		String strGoalDetailId = getRequest().getParameter("id");
		try {
			if (id != null && id > 0) {
				GoalDetail saveGoalDetail = new GoalDetail();
				saveGoalDetail.setId(id);
				BeanUtils.setProperty(saveGoalDetail, fieldName, fieldValue);
				goalDetailService.update(saveGoalDetail);
			}
			outJsonString(RESULT_SUCCESS);
		} catch (Exception e) {
			outJsonString(RESULT_FAIL);
		}

		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public static void main(String[] args) {
		GoalDetail goalDetail = new GoalDetail();
		goalDetail.setId(1L);
		// goalDetail.setName("linlinyu");
		JSONObject json = JSONObject.fromObject(goalDetail);

		System.out.println(json);

		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { "creator", "createTime", ",modifier",
				"modifyTime", "isDeleted", "new", });

		JSONObject jsonObj = JSONObject.fromObject(goalDetail, config);
		System.out.println(jsonObj.toString());
	}

	public void setGoalDetailService(GoalDetailService goalDetailService) {
		this.goalDetailService = goalDetailService;
	}

	public GoalDetail getGoalDetail() {
		return goalDetail;
	}

	public void setGoalDetail(GoalDetail goalDetail) {
		this.goalDetail = goalDetail;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

}
